package conan.presentation;

import conan.domain.Controller;

import conan.util.Log;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class OpretKompetenceFrame
    extends JFrame
{
    
    private Log log = new Log(this);
    private Controller control;
    private JTextField jTextField1 = new JTextField();
    private JLabel jLabel1 = new JLabel();
    private JButton opretKompetenceButton = new JButton();
    private JButton lukButton = new JButton();

    public OpretKompetenceFrame()
    {
        try
        {
            try
            {
                jbInit();
                JFrame frame = this;
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                Dimension frameSize = frame.getSize();
                if (frameSize.height > screenSize.height)
                {
                    frameSize.height = screenSize.height;
                }
                if (frameSize.width > screenSize.width)
                {
                    frameSize.width = screenSize.width;
                }
                frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
                //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                addWindowListener(new WindowAdapter()
                    {
                        public void windowClosing(WindowEvent e)
                        {
                            luk();
                        }
                    });
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void jbInit()
        throws Exception
    {
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(259, 132));
        this.setTitle("Opret Kompetence");
        this.setResizable(false);
        jTextField1.setBounds(new Rectangle(25, 30, 200, 25));
        jTextField1.setSize(new Dimension(200, 25));
        jLabel1.setText("Kompetence Navn");
        jLabel1.setBounds(new Rectangle(25, 5, 200, 20));
        jLabel1.setFont(new Font("Tahoma", 1, 11));
        jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
        jLabel1.setSize(new Dimension(200, 25));
        opretKompetenceButton.setText("Opret");
        opretKompetenceButton.setBounds(new Rectangle(25, 65, 80, 25));
        opretKompetenceButton.setHorizontalTextPosition(SwingConstants.CENTER);
        opretKompetenceButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    OpretKompetenceButon_actionPerformed(e);
                }
            });
        lukButton.setText("Luk");
        lukButton.setBounds(new Rectangle(145, 65, 80, 25));
        lukButton.setSize(new Dimension(80, 25));
        lukButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    LukButton_actionPerformed(e);
                }
            });
        this.getContentPane().add(lukButton, null);
        this.getContentPane().add(opretKompetenceButton, null);
        this.getContentPane().add(jLabel1, null);
        this.getContentPane().add(jTextField1, null);
    }

    public void aabn(Controller control)
    {
        this.control = control;
        this.control.useCaseStart();
        this.setVisible(true);
    }

    private void OpretKompetenceButon_actionPerformed(ActionEvent e)
    {
        if (control.gemNewKompetence(jTextField1.getText()))
        {
           log.sendMsg("Kompetence gemt i DB");
            this.setVisible(false);
            ((JFrame) this).dispose();
        }
    }

    private void LukButton_actionPerformed(ActionEvent e)
    {
        luk();
        ((JFrame) this).dispose();
    }

    private void luk()
    {
        control.useCaseAfslutUdenCommit(); // temp
    }

}
